<?php
require_once('../../ponto/estrutura/DbConnection.inc');
require_once('../../ponto/estrutura/Query.inc');
Class Leitora {
    protected $oQuery;
    public function __construct() { 
        global $_CONFIG_SERVICE;
        if (!isset($_CONFIG_SERVICE)) {
            require ('config.inc');            
        }
        $oConnexao = new DbConnection($_CONFIG_SERVICE['db_host'], 
                                      $_CONFIG_SERVICE['db_port'], 
                                      $_CONFIG_SERVICE['db_user'], 
                                      $_CONFIG_SERVICE['db_passorwd'], 
                                      $_CONFIG_SERVICE['db_database']);
        $this->oQuery = new Query($oConnexao);
    }
    
    public function getAllRegistros(){
        $this->oQuery->setSql('select * from tbregistroleitora');
        $this->oQuery->execute();
        $aRetorno = Array();
        while($oRes = $this->oQuery->fetchObject()) {
            $aRetorno[] = Array('funcodigo'=>$oRes->funcodigo,
                                'regsequencia'=>$oRes->regsequencia,
                                'regdata'=>$oRes->regdata,
                                'reghora'=>$oRes->reghora,
                                'regstatus'=>$oRes->regstatus);
        }
        $this->oQuery->freeResult();
        return $aRetorno;
    }
	
    public function getAllRegistrosWithLimit($iLimit=10) {
	$this->oQuery->setSql('select * from tbregistroleitora limit '.$iLimit);
        $this->oQuery->execute();
        $aRetorno = Array();
        while($oRes = $this->oQuery->fetchObject()) {
            $aRetorno[] = Array('funcodigo'=>$oRes->funcodigo,
                                'regsequencia'=>$oRes->regsequencia,
                                'regdata'=>$oRes->regdata,
                                'reghora'=>$oRes->reghora,
                                'regstatus'=>$oRes->regstatus);
        }
        $this->oQuery->freeResult();
        return $aRetorno;
    }
    
    public function getAllRegistrosWithLimitWhere($iLimit=10, $where = 'regstatus = 0') {
	$this->oQuery->setSql('select * from tbregistroleitora where '.$where.' limit '.$iLimit);
        $this->oQuery->execute();
        $aRetorno = Array();
        while($oRes = $this->oQuery->fetchObject()) {
            $aRetorno[] = Array('funcodigo'=>$oRes->funcodigo,
                                'regsequencia'=>$oRes->regsequencia,
                                'regdata'=>$oRes->regdata,
                                'reghora'=>$oRes->reghora,
                                'regstatus'=>$oRes->regstatus);
        }
        $this->oQuery->freeResult();
        return $aRetorno;
    }
    
    public function getAllRegistrosByFuncionario($iCodigoFuncionario){
        $this->oQuery->setSql('select * from tbregistroleitora where funcodigo = '.$iCodigoFuncionario);
        $this->oQuery->execute();
        $aRetorno = Array();
        while($oRes = $this->oQuery->fetchObject()) {
            $aRetorno[] = Array('funcodigo'=>$oRes->funcodigo,
                                'regsequencia'=>$oRes->regsequencia,
                                'regdata'=>$oRes->regdata,
                                'reghora'=>$oRes->reghora);
        }
        $this->oQuery->freeResult();
        return $aRetorno;
    }
    
    public function deleteAllRegistros(){
        $bOk = true;
        $this->oQuery->setSql('delete from tbregistroleitora');
        if(!$this->oQuery->execute()){
            $bOk = false;
        }   
        $this->oQuery->freeResult();
        
        return $bOk;
    }
    
	
    public function deleteAllRegistrosBySequencias($aSequencias) {
        $bOk = true;    
        if (count($aSequencias) > 0) {
            $sWhere = implode(',', $aSequencias);
            $this->oQuery->setSql('delete from tbregistroleitora where regsequencia in ('.$sWhere.')');
            if(!$this->oQuery->execute()){
                $bOk = false;
            }   
            $this->oQuery->freeResult();
        }
        
        return $bOk;
    }
    
    public function deleteRegistroByUsuario($iCodigoFuncionario){
        $bOk = true;
        $this->oQuery->setSql('delete from tbregistroleitora where funcodigo = '.$iCodigoFuncionario);
        if(!$this->oQuery->execute()){
            $bOk = false;
        }   
        $this->oQuery->freeResult();
        
        return $bOk;
    }
    
    public function deleteRegistroByUsuarioSequencia($iCodigoFuncionario, $iSequencia){
        $bOk = true;
        $this->oQuery->setSql('delete from tbregistroleitora where funcodigo = '.$iCodigoFuncionario.' and regsequencia = '.$iSequencia);
        if(!$this->oQuery->execute()){
            $bOk = false;
        }   
        $this->oQuery->freeResult();
        
        return $bOk;
    }
    
    
    public function deleteRegistrosByCondition($sWhere){
        $bOk = true;
        $this->oQuery->setSql('delete from tbregistroleitora where '.$sWhere);
        if(!$this->oQuery->execute()){
            $bOk = false;
        }   
        $this->oQuery->freeResult();
        
        return $bOk;
    }
    
    public function updateStatusBySequencia($iSequencia, $iStatus = 1){
        $bOk = true;
        $this->oQuery->setSql('update tbregistroleitora set regstatus = '.$iStatus.' where regsequencia = '.$iSequencia);
        if(!$this->oQuery->execute()){
            $bOk = false;
        }   
        $this->oQuery->freeResult();
        
        return $bOk;
    }
    
    public function updateStatusWhere($sWhere, $iStatus = 1){
        $bOk = true;
        $this->oQuery->setSql('update tbregistroleitora set regstatus = '.$iStatus.' where '.$sWhere);
        if(!$this->oQuery->execute()){
            $bOk = false;
        }   
        $this->oQuery->freeResult();
        
        return $bOk;
    }
    
    public function updateStatusAll($iStatus = 1){
        $bOk = true;
        $this->oQuery->setSql('update tbregistroleitora set regstatus = '.$iStatus);
        if(!$this->oQuery->execute()){
            $bOk = false;
        }   
        $this->oQuery->freeResult();
        
        return $bOk;
    }
}





        
